const express = require('express')
const fs = require('fs')
const bodyParser = require('body-parser')
const app = express()
const data_dir_path = './02/data/'
const port = 3000

app.use(bodyParser.urlencoded({
  extend: false
}))
app.use(bodyParser.json())
app.use(express.static(__dirname + '/02'));

app.get('/', (req, res) => {
  res.sendFile(__dirname + "/02/" + "index.html")
})

app.get('/home', (req, res) => {
  res.sendFile(__dirname + "/02/" + "home.html")
})

app.get('/login', (req, res) => {
  res.sendFile(__dirname + "/02/" + "login.html")
})

app.get('/register', (req, res) => {
  res.sendFile(__dirname + "/02/" + "register.html")
})

app.get('/register_validation', (req, res) => {
  let queryData = req.query

  if (queryData.uname == undefined) {
    return res.send('3')
  }

  fs.readFile(data_dir_path + 'data.json', 'utf8', (err, data) => {
    let arrData = JSON.parse(data);
    name = arrData.filter(item => {
      return item.uname == queryData.uname
    })


    if (name.toString()) {
      return res.send('0')
    } else {
      return res.send('1')
    }

  })

})

app.post('/register', (req, res) => {
  let reqBady = req.body
  if (reqBady.uname == undefined || reqBady.upassword == undefined) {
    return res.send('0')
  } else if (!(/^[A-Za-z]{4,20}$/.test(reqBady.uname) && /^[A-Za-z0-9]{6,12}$/.test(reqBady.upassword))) {
    return res.send('3')
  }

  fs.readFile(data_dir_path + 'data.json', 'utf8', (err, data) => {
    let arrData = JSON.parse(data);
    let lock = false;
    arrData.forEach(item => {
      if (item.uname == reqBady.uname) {
        lock = true;
      }
    })
    if (lock) {
      return res.send('4');
    } else {
      arrData.push({
        uname: reqBady.uname,
        upassword: reqBady.upassword
      })
      fs.readFile(data_dir_path + 'index.json', 'utf8', (err, data) => {
        let indexData = JSON.parse(data)
        indexData[reqBady.uname] = {
          pid: indexData.id ++,
          name: '',
          sex: 0,
          age: 0,
          mail:'',
          phone:''
        }
      fs.writeFile(data_dir_path + 'index.json', JSON.stringify(indexData), err => {})
      })

      fs.writeFile(data_dir_path + 'data.json', JSON.stringify(arrData), err => {
        res.send('1')
      })
    }
  })
})

app.post('/login', (req, res) => {
  let reqBady = req.body
  if (reqBady.uname == undefined || reqBady.upassword == undefined) {
    return res.send('0')
  } else if (!(/^[A-Za-z]{4,20}$/.test(reqBady.uname) && /^[A-Za-z0-9]{6,12}$/.test(reqBady.upassword))) {
    return res.send('3')
  }

  fs.readFile(data_dir_path + 'data.json', 'utf8', (err, data) => {
    let arrData = JSON.parse(data);
    let lock = false;
    arrData.forEach(item => {
      if (item.uname == reqBady.uname && item.upassword == reqBady.upassword) {
        lock = true;
      }
    })
    if (lock) {
      return res.send('1')
    } else {
      return res.send('2')
    }
  })
})

app.get('/index_getdata', (req,res) => {
  let uname = req.query.uname
  if(!uname) {
    return res.send('6')
  }
  fs.readFile(data_dir_path + 'index.json', 'utf8', (err, data) => {
    let indexData = JSON.parse(data)
    console.log(indexData,uname,indexData[uname])
    if(indexData[uname]) {
      res.send(JSON.stringify(indexData[uname]))
    } else {
      res.send('5')
    }
  })
})



app.listen(port, () => console.log(`http://127.0.0.1:${port}`))